VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "clsText"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
'====================================================================================
'clsText - A text string rendered with our custom fonts class (clsFont)
'====================================================================================
'Using the Font class to draw the text onto the screen every frame is slow (because
'the fonts are all 32-bit). This class lets us buffer the rendered text we want to
'display first
Option Explicit

Private Buffer As New ImageTGA    'The back buffer the text is kept in
Private MyFontStyle As Long       'The font style (see clsFont)
Private MyText As String          'The text to display
Private MyDimensions As PM_XY     'Width & Height

Private MyDefaultFont As clsFont
Private Const DEFAULT_FONT = "Menu"

'====================================================================================
'CLASS Initialize
'====================================================================================
Private Sub Class_Initialize()
    'Set the two buffers to show normal and highlighted font stlyes by default
    MyFontStyle = 1
    'Start with default font
    Set MyDefaultFont = ChangeFont(DEFAULT_FONT)
End Sub

'GET FontStyle: The font style to use, see clsFont
'====================================================================================
Public Property Get FontStyle() As Long: FontStyle = MyFontStyle: End Property

'LET FontStyle: Font style in use, see clsFont
'====================================================================================
Public Property Let FontStyle(ByVal NewValue As Long)
    'If the value does not differ, then leave
    If NewValue = MyFontStyle Then Exit Property
    
    'Apply new value
    MyFontStyle = NewValue
    'Rebuffer the text
    p_BufferText MyText
End Property

'SET DefaultFont: Set the font to use for the text (as a clsFont class)
'====================================================================================
Public Property Set DefaultFont(ByRef FontClass As clsFont)
    Set MyDefaultFont = FontClass
    p_BufferText MyText
End Property

'GET Text: Return the text string this class is buffering
'====================================================================================
Public Property Get Text() As String: Text = MyText: End Property

'LET Text: Set the text to render into the buffer
'====================================================================================
Public Property Let Text(ByVal NewValue As String)
    'If the text does not differ, then leave
    If NewValue = MyText Then Exit Property
    
    MyText = NewValue
    p_BufferText MyText
End Property

'GET Width: Return the width of the buffered text in pixels
'====================================================================================
Public Property Get Width() As Long: Width = MyDimensions.X: End Property

'GET Height: Return the height of the buffered text in pixels
'====================================================================================
Public Property Get Height() As Long: Height = MyDimensions.Y: End Property

'GET hDC: Return the device context handle of the buffered image
'====================================================================================
Public Property Get hDC() As Long: hDC = Buffer.hDC: End Property

'====================================================================================
'PRIVATE BufferText: Render the text into a back buffer
'====================================================================================
Private Sub p_BufferText(ByVal Text As String)
    'If no text is set, then return a blank canvas
    If Text = "" Then
        Buffer.Create 0, 0
        Exit Sub
    End If
    
    'Get the demensions of the text
    MyDimensions = GameText.GetTextDimensions(MyDefaultFont, Text, MyFontStyle)
    
    'Create the buffer to hold the text
    Buffer.Create MyDimensions.X, MyDimensions.Y
    
    'Draw the text onto it
    GameText.DrawText Buffer.hDC, 0, 0, MyDimensions.Y, MyDefaultFont, Text, MyFontStyle
End Sub

'====================================================================================
'Draw: Draw the back buffer onto the provided surface
'====================================================================================
Public Sub Draw(ByVal DesthDC As Long, ByVal X As Long, ByVal Y As Long)
    'The X and Y location to draw in are properties to this class. This is done so
    'that this class knows at all time where the text is within the screen, for
    'hit testing (mouseover)
    Buffer.DrawEx DesthDC, X, Y, Buffer.Width, Buffer.Height, 0, 0
End Sub

'====================================================================================
'CLASS Terminate
'====================================================================================
Private Sub Class_Terminate()
    Set Buffer = Nothing
    Set MyDefaultFont = Nothing
End Sub

'=== END ============================================================================
'This work is licenced under the Creative Commons Attribution 2.5 License
